package com.imast.android.bys.ui.fragment;

import java.util.List;

import com.imast.android.bys.R;
import com.imast.android.bys.adapter.CategoriaPageAdapter;
import com.imast.android.bys.io.model.Categoria;
import com.imast.android.bys.task.SoapObjectTaskFragment;
import com.imast.android.bys.util.SoapClient;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.View;

public class MedicamentoHelper {
	
	private static final String TAG_TASK_FRAGMENT = "taskSoap_fragment";
	
	private SoapObjectTaskFragment mTaskFragment;
	
	private MedicamentoFragment fragment;
	private FragmentActivity mActivity;
	private ActionBar actionBar;
	
	private CategoriaPageAdapter mCategoriaPageAdapter;
    private ViewPager mViewPager;
    
    public MedicamentoFragment getFragment() {
		return fragment;
	}
		
	public FragmentActivity getActivity() {
		return mActivity;
	}

	public ActionBar getActionBar() {
		return actionBar;
	}
	
	public SoapObjectTaskFragment getTaskFragment() {
		return mTaskFragment;
	}

	public void setTaskFragment(SoapObjectTaskFragment mTaskFragment) {
		this.mTaskFragment = mTaskFragment;
	}

	public MedicamentoHelper(MedicamentoFragment fragment, FragmentActivity mActivity, View rootView) {
		this.fragment = fragment;
		this.mActivity = mActivity;
		this.actionBar = ((ActionBarActivity)mActivity).getSupportActionBar();
		
        mViewPager = (ViewPager) rootView.findViewById(R.id.pager);
       
		initConfig();
		
	}
	
	public void onActivityCreated(){
		
		FragmentManager fm = getActivity().getSupportFragmentManager();
		mTaskFragment = (SoapObjectTaskFragment) fm.findFragmentByTag(TAG_TASK_FRAGMENT);

		if (mTaskFragment == null) {
			mTaskFragment = new SoapObjectTaskFragment();
			mTaskFragment.setTargetFragment(getFragment(), 0);
			fm.beginTransaction().add(getTaskFragment(), TAG_TASK_FRAGMENT)
					.commit();
		}
		
	}
	
	private void initConfig(){
		getCategorias();
		//mCategoriaPageAdapter = new CategoriaPageAdapter(getActivity().getSupportFragmentManager(), getCategorias());
		//mViewPager.setAdapter(mCategoriaPageAdapter);
	}
	
	private void getCategorias(){
		
		FragmentManager fm = getActivity().getSupportFragmentManager();
		mTaskFragment = (SoapObjectTaskFragment) fm.findFragmentByTag(TAG_TASK_FRAGMENT);

		String methodName = "ObtenerCategorias";
		String soapAction = "http://tempuri.org/IServiciosByS/ObtenerCategorias";
		SoapClient soapClient = new SoapClient(methodName, soapAction);

		mTaskFragment = new SoapObjectTaskFragment();
		mTaskFragment.setTargetFragment(getFragment(), 0);
		fm.beginTransaction().add(mTaskFragment, TAG_TASK_FRAGMENT).commit();
		mTaskFragment.setSoapClient(soapClient);
		
		if (!mTaskFragment.isRunning()){
			mTaskFragment.start();
		}
		
		/*List<Categoria> lstCategoria = new ArrayList<Categoria>();
		lstCategoria.add(new Categoria(1, "Vitaminas"));
		lstCategoria.add(new Categoria(2, "Belleza"));
		lstCategoria.add(new Categoria(3, "Otros"));
		
		return lstCategoria;*/
	}
	
	public void setAdapterCategorias(List<Categoria> lstCategoria){
		mCategoriaPageAdapter = new CategoriaPageAdapter(getActivity().getSupportFragmentManager(), lstCategoria);
		mViewPager.setAdapter(mCategoriaPageAdapter);
	}

}
